package com.iurac.recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iurac.recruit.entity.Hr;
import com.iurac.recruit.entity.Resume;
import com.iurac.recruit.entity.User;
import com.iurac.recruit.mapper.ResumeMapper;
import com.iurac.recruit.service.ResumeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iurac.recruit.util.Result;
import com.iurac.recruit.vo.CandidateVo;
import com.iurac.recruit.vo.PageResultVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author iurac
 * @since 2021-06-03
 */
@Service("resumeService")
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {

    @Autowired
    private ResumeMapper resumeMapper;

    @Autowired
    private HrServiceImpl hrService;

    @Override
    public PageResultVo<Resume> getByCondition(Long page, Long limit, String username) {
        PageResultVo<Resume> pageResultVo = new PageResultVo<>();
        pageResultVo.setRecords(resumeMapper.getByCondition((page-1)*limit,limit,username));
        pageResultVo.setTotal(resumeMapper.getCountByCondition(username));
        return pageResultVo;
    }

    @Override
    public Resume getByUserId(String id) {
        return baseMapper.selectOne(new QueryWrapper<Resume>()
            .eq("user_id",id)
            .last("limit 1"));
    }

    @Override
    public PageResultVo<CandidateVo> getCandidateByCondition(Long page, Long limit, String jobName, String education) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<Hr> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",user.getId());
        Hr hr = hrService.getOne(queryWrapper1);
        String companyId = hr.getCompanyId();
        PageResultVo<CandidateVo> pageResultVo = new PageResultVo<>();
        pageResultVo.setRecords(resumeMapper.getCandidateByCondition((page-1)*limit,limit,jobName,education,companyId));
        pageResultVo.setTotal(resumeMapper.getCandidateCountByCondition(jobName,education,companyId));
        return pageResultVo;
    }

    @Override
    public Result markUser(String userId,String jobId) {
        return resumeMapper.markUser(userId,jobId) > 0 ? Result.succ("标记成功") : Result.fail("标记失败");
    }

    @Override
    public Result inviteWritten(String userId, String jobId) {
        return resumeMapper.inviteWritten(userId,jobId) > 0 ? Result.succ("邀请成功") : Result.fail("邀请失败");
    }

    @Override
    public Result inviteInterview(String userId, String jobId) {
        return resumeMapper.inviteInterview(userId,jobId) > 0 ? Result.succ("邀请成功") : Result.fail("邀请失败");
    }
}
